package com.hc.common.utils;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

public class StringUtil {
    //首字母转小写
    public static String first2LowerCase(String s) {
        if (Character.isLowerCase(s.charAt(0))) {
            return s;
        } else {
            return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
        }
    }

    //首字母转大写
    public static String first2UpperCase(String s) {
        if (Character.isUpperCase(s.charAt(0))) {
            return s;
        } else {
            return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
        }
    }

    /**
     * 字符串首字母大写
     *
     * @param str
     * @return
     */
    public static String firstUpper(String str) {
        //     str = str.substring(0, 1).toUpperCase() + str.substring(1);
        //    return  str;
        char[] cs = str.toCharArray();
        cs[0] -= 32;
        return String.valueOf(cs);
    }

    /**
     * InputStream转换为字符串
     */
    public static String inputStream2String(InputStream is) throws Exception {  //字节流转为字符流
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        StringBuffer buffer = new StringBuffer();
        String line = "";
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }
        reader.close();
        return buffer.toString();
    }

    /**
     * 字符串转换为InputStream
     */
    public InputStream string2InputStream(String str) {
        ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes());
        return stream;
    }

    /**
     * 交叉将两个参数指定的字符串并返回
     *
     * @param content
     * @param salt
     * @return
     */
    public static String daSan(String content, String salt) {
        String res = "";
        int len = content.length() > salt.length() ? salt.length() : content.length();
        int i = 0;
        for (; i < len; i++) {
            res = res + content.charAt(i) + salt.charAt(i);
        }
        if (content.length() > salt.length()) {
            res += content.substring(i);
        } else {
            res += salt.substring(i);
        }
        return res;
    }

    public static void main(String[] args) {
        System.out.println(daSan("asdfghj", "123579"));
        System.out.println(daSan("asdf", "123579"));
    }

}
